[PR]

『新妻LOVELY×CATION』を応援しています!
水無瀬の部屋 > Programming > sample > tools > misc > md4.cpp
最終更新日: 2007/03/24

   1: //*********************************************************
   2: // プロジェクト: TOOLS::MD4 - Message-Digest Algorithm
   3: //   ファイル名: md4.cpp
   4: //*********************************************************
   5: #include <misc/md4.h>        //
   6: #include <header/tooldbg.h>  // ASSERT(), 
   7: #include <header/toolbase.h> // 
   8: #include <header/snprintf.h> // snprintf(), vsnprintf(), 
   9: #include <limits.h>          // CHAR_BIT, 
  10: 
  11: 
  12: //---------------------------------------------------------
  13: // テスト関数 の 宣言
  14: //---------------------------------------------------------
  15: DECLARE_TESTPROC( test_md4 );
  16: 
  17: 
  18: //---------------------------------------------------------
  19: // 定数型マクロ の 定義
  20: //---------------------------------------------------------
  21: #define DBG_ALLOCNAME  "MD4_CreateAlgorithm"
  22: 
  23: //
  24: #define S11  3
  25: #define S12  7
  26: #define S13  11
  27: #define S14  19
  28: #define S21  3
  29: #define S22  5
  30: #define S23  9
  31: #define S24  13
  32: #define S31  3
  33: #define S32  9
  34: #define S33  11
  35: #define S34  15
  36: 
  37: 
  38: //---------------------------------------------------------
  39: // 関数型マクロ の 定義
  40: //---------------------------------------------------------
  41: // F, G, H are basic MD4 functions.
  42: static inline DWORD F( DWORD x, DWORD y, DWORD z ){ return (x & y) | (~x & z); }
  43: static inline DWORD G( DWORD x, DWORD y, DWORD z ){ return (x & y) | (x & z) | (y & z); }
  44: static inline DWORD H( DWORD x, DWORD y, DWORD z ){ return x ^ y ^ z; }
  45: 
  46: // ROTATE_LEFT rotates x left n bits.
  47: static inline DWORD ROTATE_LEFT( DWORD x, DWORD n ){ return (x << n) | (x >> ((CHAR_BIT * sizeof(DWORD)) - n)); }
  48: 
  49: // MD4_MAKEDWORD
  50: static inline DWORD MD4_MAKEDWORD( BYTE b1, BYTE b2, BYTE b3, BYTE b4 ){ return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24); }
  51: 
  52: 
  53: //---------------------------------------------------------
  54: // 構造体 の 宣言
  55: //---------------------------------------------------------
  56: typedef struct MD4_CTX_tag
  57: {
  58: 	DWORD state[ 4 ];   // 16bits
  59: 	DWORD count[ 2 ];   //  8bits
  60: 	BYTE  buffer[ 64 ]; // 64bits
  61: } MD4_CTX;
  62: 
  63: 
  64: //---------------------------------------------------------
  65: // ファイルスコープ関数 の 宣言
  66: //---------------------------------------------------------
  67: static void MD4_Transform( DWORD state[4], const BYTE block[64], int count );
  68: static bool MD4_Encode( BYTE *output, int bufsize, const DWORD *input, int len );
  69: static void MD4_Decode( DWORD *output, int bufsize, const BYTE *input, int len );
  70: static bool MD4_IsValidContext( const MD4_CTX *context );
  71: 
  72: 
  73: //---------------------------------------------------------
  74: // ファイルスコープ変数 の 定義
  75: //---------------------------------------------------------
  76: static const BYTE PADDING[ 64 ] =
  77: 	{
  78: 		0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79: 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80: 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81: 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  82: 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83: 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84: 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85: 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  86: 	};
  87: 
  88: 
  89: //*********************************************************
  90: // MD4_CreateAlgorithm
  91: //*********************************************************
  92: MD4_CTX *
  93: MD4_CreateAlgorithm
  94: 	(
  95: 		void
  96: 	)
  97: {
  98: 	CALLONCE_TESTPROC( test_md4 ); // [テスト]
  99: 
 100: 	MD4_CTX *context = (MD4_CTX *)malloc( sizeof( *context ) );
 101: 	if ( !context )
 102: 		return null;
 103: 
 104: 	MD4_InitData( context );
 105: 	ASSERT( MD4_IsValidContext( context ) );
 106: 	
 107: 	DBG_LOCK_ALLOCED_MEMORY( context, DBG_ALLOCNAME ); // [DBG]
 108: 	return context;
 109: }//MD4_CreateAlgorithm
 110: 
 111: //*********************************************************
 112: // MD4_DestroyAlgorithm
 113: //*********************************************************
 114: bool
 115: MD4_DestroyAlgorithm
 116: 	(
 117: 		MD4_CTX *context
 118: 	)
 119: {
 120: 	// パラメタの仮定
 121: 	ASSERT( MD4_IsValidContext( context ) );
 122: 
 123: 	DBG_UNLOCK_ALLOCED_MEMORY( context, DBG_ALLOCNAME ); // [DBG]
 124: 	free( context );
 125: 
 126: 	return true;
 127: }//MD4_DestroyAlgorithm
 128: 
 129: //*********************************************************
 130: // MD4_InitData
 131: //*********************************************************
 132: bool
 133: MD4_InitData
 134: 	(
 135: 		MD4_CTX *context
 136: 	)
 137: {
 138: 	// パラメタの仮定
 139: 	ASSERT( IsValidPtr( context, sizeof( *context ) ) );
 140: 
 141: 	context->count[ 0 ] = 0;
 142: 	context->count[ 1 ] = 0;
 143: 
 144: 	context->state[ 0 ] = 0x67452301;
 145: 	context->state[ 1 ] = 0xefcdab89;
 146: 	context->state[ 2 ] = 0x98badcfe;
 147: 	context->state[ 3 ] = 0x10325476;
 148: 
 149: 	ASSERT( MD4_IsValidContext( context ) );
 150: 	return true;
 151: }//MD4_InitData
 152: 
 153: //*********************************************************
 154: // MD4_AddData
 155: //*********************************************************
 156: bool
 157: MD4_AddData
 158: 	(
 159: 		      MD4_CTX *context,
 160: 		const void    *data,
 161: 		      int      length
 162: 	)
 163: {
 164: 	// パラメタの仮定
 165: 	ASSERT( MD4_IsValidContext( context ) );
 166: 	ASSERT( 0 < length );
 167: 	ASSERT( IsValidReadPtr( data, length ) );
 168: 
 169: 	//
 170: 	const BYTE *p = (BYTE *)data;
 171: 	const UINT index = static_cast<UINT>( 0x3F & (context->count[ 0 ] >> 3) );
 172: 
 173: 	//
 174: 	context->count[ 0 ] += ((DWORD)length << 3);
 175: 	if ( context->count[ 0 ] < ((DWORD)length << 3) )
 176: 	{
 177: 		context->count[ 1 ]++;
 178: 	}
 179: 	context->count[ 1 ] += ((DWORD)length >> 29);
 180: 
 181: 	//
 182: 	const UINT partLen = 64 - index;
 183: 	
 184: 	//
 185: 	if ( (DWORD)length < partLen )
 186: 	{
 187: 		memcpy( &context->buffer[ index ], &p[ 0 ], length );
 188: 	}
 189: 	else
 190: 	{
 191: 		memcpy( &context->buffer[ index ], p, partLen );
 192: 		MD4_Transform( context->state, context->buffer, 64 );
 193: 
 194: 		UINT i;
 195: 		for( i = partLen; i + 63 < (DWORD)length; i += 64 )
 196: 		{
 197: 			MD4_Transform( context->state, &p[ i ], 64 );
 198: 		}
 199: 
 200: 		memcpy( &context->buffer[ 0 ], &p[ i ], length-i );
 201: 	}
 202: 
 203: 	return true;
 204: }//MD4_AddData
 205: 
 206: //*********************************************************
 207: // MD4_GetHash
 208: // bufsize = numof(bits)
 209: //*********************************************************
 210: bool
 211: MD4_GetHash
 212: 	(
 213: 		const MD4_CTX *context,
 214: 		      void    *digest, 
 215: 		      int      bufsize
 216: 	)
 217: {
 218: 	// パラメタの仮定
 219: 	ASSERT( MD4_IsValidContext( context ) );
 220: 	ASSERT( 0 < bufsize );
 221: 	ASSERT( IsValidPtr( digest, bufsize ) );
 222: 	DESTROY_BUFFER( digest, bufsize );
 223: 
 224: 	//
 225: 	BYTE bits[ 8 ];
 226: 	MD4_Encode( bits, sizeof( bits ), context->count, numof( context->count ) );
 227: 	const UINT index  = static_cast<UINT>( 0x3F & (context->count[ 0 ] >> 3) );
 228: 	const UINT padLen = (index < 56) ? (56 - index) : (120 - index);
 229: 
 230: 	MD4_CTX tmp_context;
 231: 	memcpy( &tmp_context, context, sizeof( *context ) );
 232: 	context = null;
 233: 
 234: 	MD4_AddData( &tmp_context, PADDING, padLen );
 235: 	MD4_AddData( &tmp_context, bits, sizeof( bits ) );
 236: 	MD4_Encode( (BYTE *)digest, bufsize, tmp_context.state, numof( tmp_context.state ) );
 237: 
 238: 	return true;
 239: }//MD4_GetHash
 240: 
 241: //*********************************************************
 242: // MD4_GetHashText
 243: //*********************************************************
 244: bool
 245: MD4_GetHashText
 246: 	(
 247: 		const MD4_CTX *context,
 248: 		      char    *buffer,
 249: 			  int      bufsize
 250: 	)
 251: {
 252: 	// パラメタの仮定
 253: 	ASSERT( MD4_IsValidContext( context ) );
 254: 	ASSERT( 0 < bufsize );
 255: 	ASSERT( IsValidStringBufferPtr( buffer, bufsize ) );
 256: 	DESTROY_TEXT_BUFFER( buffer, bufsize );
 257: 
 258: 	//
 259: 	BYTE md4[ MD4_HASHSIZE ];
 260: 	VERIFY( MD4_GetHash( context, md4, sizeof( md4 ) ) );
 261: 
 262: 	//
 263: 	char text[ 1+MD4_HASHTEXTSIZE ];
 264: 	{
 265: 		char *p = text;
 266: 		{for( int i = 0; i < numof( md4 ); ++i )
 267: 		{
 268: 			snprintf( p, numof(text) - (p - text), "%02x", md4[ i ] );
 269: 			p = strtail( p );
 270: 		}}
 271: 	}
 272: 
 273: 	//
 274: 	memcpy( buffer, text, min( bufsize, sizeof( text ) ) );
 275: 	buffer[ min( bufsize, numof( text ) ) - 1 ] = '\0';
 276: 
 277: 
 278: 	return true;
 279: }//MD4_GetHashText
 280: 
 281: 
 282: //******************************************************************************************************************
 283: //
 284: //******************************************************************************************************************
 285: static inline DWORD R1( DWORD a, DWORD b, DWORD c, DWORD d, DWORD x, DWORD s ){ a += F( b, c, d ) + x; a = ROTATE_LEFT( a, s ); return a; }
 286: static inline DWORD R2( DWORD a, DWORD b, DWORD c, DWORD d, DWORD x, DWORD s ){ a += G( b, c, d ) + x + 0x5a827999; a = ROTATE_LEFT( a, s ); return a; }
 287: static inline DWORD R3( DWORD a, DWORD b, DWORD c, DWORD d, DWORD x, DWORD s ){ a += H( b, c, d ) + x + 0x6ed9eba1; a = ROTATE_LEFT( a, s ); return a; }
 288: 
 289: 
 290: //*********************************************************
 291: // MD4_IsValidContext
 292: //*********************************************************
 293: static
 294: bool
 295: MD4_IsValidContext
 296: 	(
 297: 		const MD4_CTX *context
 298: 	)
 299: {
 300: 	VALID_TEST( context );
 301: 	VALID_TEST( IsValidReadPtr( context, sizeof( *context ) ) );
 302: 	VALID_TEST( 0 == (0x03 & context->count[ 0 ]) );
 303: //	VALID_TEST( 0 == context->count[ 1 ] );
 304: 
 305: 	return true;
 306: }//MD4_IsValidContext
 307: 
 308: //*********************************************************
 309: // MD4_Transform
 310: //*********************************************************
 311: static
 312: void
 313: MD4_Transform
 314: 	(
 315: 		      DWORD *state,
 316: 		const BYTE  *block, 
 317: 		      int    count
 318: 	)
 319: {
 320: 	DWORD x[ 16 ];
 321: 	DWORD a = state[ 0 ];
 322: 	DWORD b = state[ 1 ];
 323: 	DWORD c = state[ 2 ];
 324: 	DWORD d = state[ 3 ];
 325: 
 326: 	MD4_Decode( x, numof( x ), block, count );
 327: 
 328: 	// Round 1
 329: 	{
 330: 		a = R1( a, b, c, d, x[ 0], S11 );
 331: 		d = R1( d, a, b, c, x[ 1], S12 ); 
 332: 		c = R1( c, d, a, b, x[ 2], S13 );
 333: 		b = R1( b, c, d, a, x[ 3], S14 );
 334: 
 335: 		a = R1( a, b, c, d, x[ 4], S11 );
 336: 		d = R1( d, a, b, c, x[ 5], S12 );
 337: 		c = R1( c, d, a, b, x[ 6], S13 );
 338: 		b = R1( b, c, d, a, x[ 7], S14 );
 339: 
 340: 		a = R1( a, b, c, d, x[ 8], S11 );
 341: 		d = R1( d, a, b, c, x[ 9], S12 );
 342: 		c = R1( c, d, a, b, x[10], S13 );
 343: 		b = R1( b, c, d, a, x[11], S14 );
 344: 
 345: 		a = R1( a, b, c, d, x[12], S11 );
 346: 		d = R1( d, a, b, c, x[13], S12 );
 347: 		c = R1( c, d, a, b, x[14], S13 );
 348: 		b = R1( b, c, d, a, x[15], S14 );
 349: 	}
 350: 
 351: 	// Round 2
 352: 	{
 353: 		a = R2( a, b, c, d, x[ 0], S21 );
 354: 		d = R2( d, a, b, c, x[ 4], S22 );
 355: 		c = R2( c, d, a, b, x[ 8], S23 );
 356: 		b = R2( b, c, d, a, x[12], S24 );
 357: 
 358: 		a = R2( a, b, c, d, x[ 1], S21 );
 359: 		d = R2( d, a, b, c, x[ 5], S22 );
 360: 		c = R2( c, d, a, b, x[ 9], S23 );
 361: 		b = R2( b, c, d, a, x[13], S24 );
 362: 
 363: 		a = R2( a, b, c, d, x[ 2], S21 );
 364: 		d = R2( d, a, b, c, x[ 6], S22 );
 365: 		c = R2( c, d, a, b, x[10], S23 );
 366: 		b = R2( b, c, d, a, x[14], S24 );
 367: 
 368: 		a = R2( a, b, c, d, x[ 3], S21 );
 369: 		d = R2( d, a, b, c, x[ 7], S22 );
 370: 		c = R2( c, d, a, b, x[11], S23 );
 371: 		b = R2( b, c, d, a, x[15], S24 );
 372: 	}
 373: 
 374: 	// Round 3
 375: 	{
 376: 		a = R3( a, b, c, d, x[ 0], S31 );
 377: 		d = R3( d, a, b, c, x[ 8], S32 );
 378: 		c = R3( c, d, a, b, x[ 4], S33 );
 379: 		b = R3( b, c, d, a, x[12], S34 );
 380: 
 381: 		a = R3( a, b, c, d, x[ 2], S31 );
 382: 		d = R3( d, a, b, c, x[10], S32 );
 383: 		c = R3( c, d, a, b, x[ 6], S33 );
 384: 		b = R3( b, c, d, a, x[14], S34 );
 385: 
 386: 		a = R3( a, b, c, d, x[ 1], S31 );
 387: 		d = R3( d, a, b, c, x[ 9], S32 );
 388: 		c = R3( c, d, a, b, x[ 5], S33 );
 389: 		b = R3( b, c, d, a, x[13], S34 );
 390: 
 391: 		a = R3( a, b, c, d, x[ 3], S31 );
 392: 		d = R3( d, a, b, c, x[11], S32 );
 393: 		c = R3( c, d, a, b, x[ 7], S33 );
 394: 		b = R3( b, c, d, a, x[15], S34 );
 395: 	}
 396: 
 397: 	state[ 0 ] += a;
 398: 	state[ 1 ] += b;
 399: 	state[ 2 ] += c;
 400: 	state[ 3 ] += d;
 401: }//MD4_Transform
 402: 
 403: //*********************************************************
 404: // MD4_Encode
 405: // length == sizeof(src)
 406: //*********************************************************
 407: static
 408: bool
 409: MD4_Encode
 410: 	(
 411: 		      BYTE  *dst,
 412: 		      int    bufsize,
 413: 		const DWORD *src, 
 414: 		      int    count
 415: 	)
 416: {
 417: 	{for( int i = 0; (0 < bufsize) && (i < count); ++i )
 418: 	{
 419: 		*dst++ = static_cast<BYTE>( 0xFF & *src );
 420: 		if ( --bufsize <= 0 )
 421: 			return true;
 422: 
 423: 		*dst++ = static_cast<BYTE>( 0xFF & (*src >> 8) );
 424: 		if ( --bufsize <= 0 )
 425: 			return true;
 426: 
 427: 		*dst++ = static_cast<BYTE>( 0xFF & (*src >> 16) );
 428: 		if ( --bufsize <= 0 )
 429: 			return true;
 430: 
 431: 		*dst++ = static_cast<BYTE>( 0xFF & (*src >> 24) );
 432: 		if ( --bufsize <= 0 )
 433: 			return true;
 434: 
 435: 		++src;
 436: 	}}
 437: 
 438: 	return true;
 439: }//MD4_Encode
 440: 
 441: //*********************************************************
 442: // MD4_Decode
 443: // length == sizeof(dst)
 444: //*********************************************************
 445: static
 446: void
 447: MD4_Decode
 448: 	(
 449: 		      DWORD *dst,
 450: 		      int    count,
 451: 		const BYTE  *src,
 452: 		      int    length
 453: 	)
 454: {
 455: 	{for( int i = 0; i < min(count, (int)(length / sizeof(DWORD))); ++i )
 456: 	{
 457: 		*dst++ = MD4_MAKEDWORD( src[ 0 ], src[ 1 ], src[ 2 ], src[ 3 ] );
 458: 		src += sizeof(DWORD);
 459: 	}}
 460: 
 461: }//MD4_Decode
 462: 
 463: 
 464: //******************************************************************************************************************
 465: // TEST
 466: //******************************************************************************************************************
 467: 
 468: 
 469: #ifdef _DEBUG // デバッグ時のみ
 470: 
 471: 
 472: //*********************************************************
 473: // test_md4
 474: //*********************************************************
 475: DEFINE_TESTPROC( test_md4 )
 476: {
 477: 	//---------------------------------------------------------
 478: 	// 定数 の テスト
 479: 	//---------------------------------------------------------
 480: 
 481: 	//---------------------------------------------------------
 482: 	// ファイルスコープ関数 の テスト
 483: 	//---------------------------------------------------------
 484: 
 485: 	//---------------------------------------------------------
 486: 	// 公開関数 の テスト
 487: 	//---------------------------------------------------------
 488: 
 489: 	static const BYTE testcase[] = 
 490: 		{
 491: 			0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
 492: 			0xCB, 0xD9, 0xCC, 0x90, 0x6B, 0xA7, 0x8C, 0xA2, 0xE6, 0xF2, 0xF4, 0x77, 0x61, 0xBC, 0x89, 0xEF,
 493: 			0xBE, 0x9F, 0x2B, 0x7D, 0x59, 0x0E, 0x0D, 0x27, 0xF9, 0x87, 0x75, 0xD2, 0x18, 0x8E, 0x2B, 0x3D,
 494: 			0x63, 0x35, 0xF4, 0x93, 0x8A, 0xCA, 0xD2, 0xF2, 0xA4, 0xB9, 0x0A, 0xDD, 0x3B, 0x21, 0x4F, 0x14,
 495: 			0x69, 0x34, 0x28, 0x4A, 0xB5, 0x64, 0xF0, 0xB5, 0x31, 0x46, 0x1A, 0x02, 0x3C, 0xE1, 0xC8, 0xC8,
 496: 			0xEE, 0x85, 0xA9, 0x98, 0x78, 0x39, 0x8B, 0x15, 0x5B, 0xF6, 0x0B, 0x79, 0xCA, 0xDA, 0xBA, 0x69,
 497: 			0x9F, 0xAB, 0xD6, 0xC2, 0x78, 0xAA, 0x30, 0x04, 0x9A, 0xD1, 0xA7, 0x05, 0x66, 0xCB, 0x01, 0x87,
 498: 			0x6C, 0xC3, 0x10, 0xCE, 0xF9, 0xF2, 0x01, 0x18, 0x03, 0xFD, 0x61, 0xF5, 0xC5, 0x92, 0xA8, 0x41,
 499: 			0x97, 0x0A, 0x86, 0x0D, 0x10, 0xB0, 0x77, 0x29, 0x4E, 0x96, 0x10, 0xC4, 0xDF, 0x26, 0xB6, 0xE7,
 500: 			0x48, 0xFD, 0xF3, 0x1A, 0x79, 0x85, 0x96, 0x82, 0x3D, 0x5A, 0xD4, 0x58, 0xB2, 0x9A, 0xC4, 0x26,
 501: 			0x02, 0xBD, 0x48, 0x6C, 0xFD, 0x9C, 0x4A, 0x87, 0xF5, 0xF6, 0x50, 0x38, 0x98, 0x74, 0x60, 0x04,
 502: 			0xB9, 0x13, 0xEE, 0x2E, 0xCD, 0x95, 0x49, 0x50, 0x61, 0x46, 0xA9, 0xCF, 0x80, 0x07, 0x22, 0x9B,
 503: 			0x6E, 0x78, 0x01, 0x95, 0xFA, 0x78, 0xFA, 0xF2, 0x4F, 0x98, 0x01, 0x36, 0xE7, 0x63, 0x0D, 0xB9,
 504: 			0x4C, 0x8F, 0x2D, 0x05, 0x8D, 0x2E, 0x34, 0x08, 0x84, 0x8B, 0x3A, 0xDB, 0x47, 0xC2, 0xF0, 0xA9,
 505: 			0x06, 0x8C, 0x16, 0xA9, 0xCB, 0x2A, 0x08, 0x04, 0xB1, 0xDF, 0xFC, 0x82, 0xF6, 0xD3, 0xE1, 0x20,
 506: 			0xA5, 0x7B, 0xDC, 0x62, 0xCE, 0x74, 0x8B, 0xBB, 0xB2, 0xFA, 0xA0, 0xFC, 0x54, 0x88, 0xBF, 0x2E,
 507: 			0xFC, 0x15, 0x0B, 0xFC, 0x15, 0x90, 0x1E, 0x54, 0xD0, 0x87, 0xDA, 0x35, 0xDB, 0x29, 0x85, 0xF4,
 508: 			0xC6, 0xD0, 0x99, 0x42, 0x0C, 0xF8, 0xD8, 0xE7, 0xBC, 0x2A, 0x8E, 0x4F, 0xFE, 0xC2, 0x32, 0xFB,
 509: 			0x4C, 0x60, 0x93, 0xB4, 0x07, 0xAB, 0x53, 0x25, 0xFD, 0x61, 0x90, 0x1D, 0xA4, 0xC9, 0xAD, 0xF0,
 510: 			0x62, 0x82, 0x1B, 0xC7, 0xD7, 0xB2, 0xB6, 0x9F, 0xB7, 0x2B, 0x68, 0x70, 0x24, 0x8D, 0x8C, 0x27,
 511: 			0x88, 0x04, 0xB1, 0xEC, 0xAE, 0x64, 0x49, 0xD0, 0x8D, 0xCF, 0x2C, 0xA8, 0xFB, 0x40, 0x1A, 0x6E,
 512: 			0xDF, 0xF1, 0x44, 0x70, 0x44, 0x49, 0xAC, 0x56, 0x56, 0x8E, 0xD4, 0x41, 0x7F, 0x89, 0x95, 0x00,
 513: 			0x23, 0xAF, 0x80, 0xD3, 0x4A, 0x3B, 0xEA, 0x77, 0x9A, 0x8E, 0x91, 0x5E, 0xEC, 0x2F, 0x69, 0xC5,
 514: 			0xEB, 0xC1, 0xFD, 0xFA, 0x58, 0x55, 0x2C, 0x61, 0x9B, 0xC3, 0x49, 0x11, 0xF3, 0xD3, 0x44, 0x84,
 515: 			0xF7, 0x1F, 0x81, 0xBE, 0x72, 0xA6, 0x4F, 0x97, 0x65, 0xDD, 0x66, 0xB2, 0x08, 0x04, 0xD7, 0xBF,
 516: 			0xDD, 0x19, 0x2B, 0xF5, 0x29, 0x87, 0x0F, 0x70, 0xED, 0xC9, 0x67, 0xAD, 0xF0, 0x6A, 0xE0, 0xD4,
 517: 			0x1A, 0x2D, 0x1A, 0x94, 0x53, 0xE8, 0x7E, 0x02, 0x86, 0x2D, 0x49, 0xA3, 0x4B, 0xF1, 0x82, 0x4E,
 518: 			0xD8, 0xB1, 0x47, 0x33, 0x2E, 0x3D, 0x52, 0x75, 0xBB, 0xC1, 0xA6, 0xEF, 0x17, 0xFD, 0x14, 0xFB,
 519: 			0x3B, 0xDD, 0x73, 0xDA, 0x1D, 0xBB, 0x3B, 0x4C, 0x6C, 0xA6, 0x58, 0xAB, 0x40, 0xA0, 0x92, 0x61,
 520: 			0xE0, 0x63, 0xA3, 0x50, 0xAC, 0x0E, 0xBF, 0xEE, 0x7D, 0xE4, 0xF2, 0x12, 0x45, 0x1D, 0xE4, 0x57,
 521: 			0x8E, 0xE7, 0x10, 0xA7, 0x78, 0xF0, 0x1F, 0x08, 0x75, 0x1B, 0xC4, 0x24, 0x61, 0xCB, 0x21, 0x4F,
 522: 			0xE8, 0xD6, 0xEB, 0x06, 0xDF, 0x0A, 0xEA, 0x11, 0x4E, 0x22, 0x74, 0xF5, 0x8E, 0x07, 0x3C, 0xB8,
 523: 		};
 524: 
 525: 	// MD4
 526: 	MD4_CTX *context = MD4_CreateAlgorithm();
 527: 
 528: 	// 一気にハッシュ
 529: 	{for( int i = 1; (i * MD4_HASHSIZE) < numof( testcase ); ++i )
 530: 	{
 531: 		MD4_InitData( context );
 532: 		MD4_AddData( context, testcase, i * MD4_HASHSIZE );
 533: 
 534: 		BYTE md4[ MD4_HASHSIZE ];
 535: 		{for( int j = 1; j < numof( md4 ); ++j )
 536: 		{
 537: 			MD4_GetHash( context, md4, j );
 538: 			VERIFY( 0 == memcmp( md4, testcase + (i * MD4_HASHSIZE), j ) );
 539: 		}}
 540: 	}}
 541: 
 542: 	// 徐々にハッシュ
 543: 	MD4_InitData( context );
 544: 	{for( int i = 1; (i * MD4_HASHSIZE) < numof( testcase ); ++i )
 545: 	{
 546: 		{for( int j = 0; j < MD4_HASHSIZE; ++j )
 547: 		{
 548: 			MD4_AddData( context, &testcase[ j + ((i-1) * MD4_HASHSIZE) ], 1 );
 549: 		}}
 550: 
 551: 		BYTE md4[ MD4_HASHSIZE ];
 552: 		MD4_GetHash( context, md4, numof( md4 ) );
 553: 		VERIFY( 0 == memcmp( md4, testcase + (i * MD4_HASHSIZE), numof( md4 ) ) );
 554: 	}}
 555: 
 556: 	VERIFY( MD4_DestroyAlgorithm( context ) );
 557: 
 558: }//test_md4
 559: 
 560: 
 561: #endif // #ifdef _DEBUG
 562: 
 563: 
 564: //** end **
 565: 

参照:


Google
ご意見・ご感想をお聞かせ下さい。匿名で送信できます。

 * 返信が必要な場合には postmaster@katsura-kotonoha.sakura.ne.jp へ直接メールしてください。

水無瀬の部屋 > sample > tools > misc > md4.cpp

このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/tools/misc/md4_cpp.shtml
>> Amazon.co.jp 『たまゆら童子』 へ
>> 楽天ブックス 『たまゆら童子』 へ